<template>
  <BasicModal
    v-bind="$attrs"
    @register="register"
    :title="type === 'scheduleCheck' ? '调度审核' : '派单审核'"
    @ok="handleSubmit"
  >
    <div class="pt-3px pr-3px">
      <BasicForm @register="registerForm" :model="modelRef" />
    </div>
  </BasicModal>
</template>
<script lang="ts" setup>
  import { reactive, ref } from 'vue';
  import { BasicModal, useModalInner } from '@/components/Modal';
  import { BasicForm, FormSchema, useForm } from '@/components/Form';

  const emit = defineEmits(['handleSave']);
  const modelRef: any = reactive({
    isPass: '',
    rejectRemark: '',
  });
  const shippingOrderId = ref('');
  const type = ref('');

  const [register, { closeModal }] = useModalInner((data) => {
    shippingOrderId.value = data.id;
    type.value = data.type;
  });

  const schemas: FormSchema[] = [
    {
      field: 'rejectRemark',
      component: 'InputTextArea',
      label: '审批意见',
      colProps: {
        span: 24,
      },
      // required: modelRef.isPass == false ? true : false,
      dynamicRules: ({ values }) => {
        return values.isPass === false ? [{ required: true, message: '审批意见' }] : [];
      },
    },
    {
      field: 'isPass',
      component: 'RadioGroup',
      label: '审批结果',
      colProps: {
        span: 24,
      },
      required: true,
      componentProps: {
        options: [
          {
            label: '通过',
            value: true,
          },
          {
            label: '不通过',
            value: false,
          },
        ],
      },
    },
  ];

  const [registerForm, { validate }] = useForm({
    labelWidth: 120,
    schemas,
    showActionButtonGroup: false,
    actionColOptions: {
      span: 24,
    },
  });

  //表单提交
  async function handleSubmit() {
    const data = await validate();
    console.log('data', data);
    emit('handleSave', { shippingOrderId: shippingOrderId.value, ...data, type: type.value });
    closeModal();
  }
</script>
